package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ClassMapper;
import com.think.teaching.model.Class;
import com.think.teaching.model.StudentInfo;
import com.think.teaching.service.ClassService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ClassServiceImpl extends BaseServiceImpl<ClassMapper, Class> implements ClassService {

	@Override
	public Class AddNewClass(Class _class) {
		if (this.save(_class)) {
			return _class;
		}
		return null;
	}

	@Override
	public Class UpdateClass(Class _class) {
		if (this.updateById(_class)) {
			return _class;
		}
		return null;
	}

	public IPage<Class> ListClassInfo(Integer page, Integer count, Integer major, Integer college, Integer school,
			Boolean withoutBan) {
		IPage<Class> ipage = new Page<>(page, count);
		if (major > 0) {
			return baseMapper.getClassList(ipage, major, -1, -1, withoutBan ? 1 : 0);

		} else if (college > 0) {
			return baseMapper.getClassList(ipage, -1, college, -1, withoutBan ? 1 : 0);

		} else if (school > 0) {
			return baseMapper.getClassList(ipage, -1, -1, school, withoutBan ? 1 : 0);
		}
		return baseMapper.getClassList(ipage, major, college, school, withoutBan ? 1 : 0);
	}

	@Override
	public Class GetClassInfo(Integer id) {
		QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public IPage<StudentInfo> ListClassStudents(Integer page, Integer count, Boolean withoutDelete) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Class SetClassState(Integer id, Integer state) {
		UpdateWrapper<Class> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public List<Class> SearchClassWithKey(String key, Boolean withoutBan) {
		QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

}
